package cn.com.cifi.mars.service.impl;

import cn.com.cifi.mars.bean.dto.MonthTradePlanDto;
import cn.com.cifi.mars.entity.MonthTradePlan;
import cn.com.cifi.mars.mapper.mars.MonthTradePlanMapper;
import cn.com.cifi.mars.service.MonthTradePlanService;
import cn.com.cifi.mars.util.BigDecimalUtils;
import cn.com.cifi.mars.util.UUIDUtils;
import lombok.extern.slf4j.Slf4j;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
@Slf4j
@Service
public class MonthTradePlanServiceImpl extends ServiceImpl<MonthTradePlanMapper,MonthTradePlan> implements MonthTradePlanService {
    @Autowired
    MonthTradePlanMapper monthTradePlanMapper;
    @Override
    public Set<String> insertMonthTradePlanData(JSONArray requestInfo) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();

        Set<String> set = new HashSet<>();
        List<MonthTradePlan> list = new ArrayList<>();
        if (null != requestInfo){
            for (Object requestInfoItem : requestInfo) {
                if (null != requestInfoItem){
                    MonthTradePlan monthTradePlan = new MonthTradePlan();
                    JSONObject itemObj = JSON.parseObject(requestInfoItem.toString());
                    String id = UUIDUtils.create();
                    monthTradePlan.setId(id);
                    monthTradePlan.setProjectId(itemObj.getString("projectID"));
                    monthTradePlan.setProjectCode(itemObj.getString("projectCode"));
                    monthTradePlan.setProjectName(itemObj.getString("projectName"));
                    monthTradePlan.setStageId(itemObj.getString("stageId"));
                    set.add(itemObj.getString("stageId"));
                    monthTradePlan.setStageName(itemObj.getString("stageName"));
                    monthTradePlan.setGroupId(itemObj.getString("groupId"));
                    monthTradePlan.setGroupName(itemObj.getString("groupName"));
                    monthTradePlan.setProductCode(itemObj.getString("productCode"));
                    monthTradePlan.setProductName(itemObj.getString("productName"));
                    monthTradePlan.setAreaName(itemObj.getString("areaName"));

                    if (null != itemObj.getInteger("newTradeNumPlan")){
                        monthTradePlan.setNewTradeNumPlan(itemObj.getBigDecimal("newTradeNumPlan"));
                    }else {
                        monthTradePlan.setNewTradeNumPlan(BigDecimal.ZERO);
                    }
                    if (StringUtils.isNotBlank(itemObj.getString("newTradeAmountPlan"))){
                        monthTradePlan.setNewTradeAmountPlan(BigDecimalUtils.multiplyBigDecimal(new BigDecimal(itemObj.getString("newTradeAmountPlan")),new BigDecimal(10000),4));
                    }else {
                        monthTradePlan.setNewTradeAmountPlan(new BigDecimal("0"));
                    }
                    if (null != itemObj.getInteger("inventoryTradeNumPlan")){
                        monthTradePlan.setInventoryTradeNumPlan(itemObj.getBigDecimal("inventoryTradeNumPlan"));
                    }else {
                        monthTradePlan.setInventoryTradeNumPlan(BigDecimal.ZERO);
                    }
                    if (StringUtils.isNotBlank(itemObj.getString("inventoryTradeAmountPlan"))){
                        monthTradePlan.setInventoryTradeAmountPlan(BigDecimalUtils.multiplyBigDecimal(new BigDecimal(itemObj.getString("inventoryTradeAmountPlan")),new BigDecimal(10000),4));
                    }else {
                        monthTradePlan.setInventoryTradeAmountPlan(new BigDecimal("0"));
                    }
                    monthTradePlan.setSupplyPlanId(itemObj.getString("supplyPlanId"));
                    String createDate1 = itemObj.getString("createDate")+" 00:00:00";
                    if (StringUtils.isNotBlank(itemObj.getString("createDate"))){
                        LocalDateTime signDate = LocalDateTime.parse(createDate1, formatter);
                        monthTradePlan.setSignDate(signDate);
                    }
                    monthTradePlan.setCreateDate(now);
                    list.add(monthTradePlan);
                }
            }

            monthTradePlanMapper.insertMonthTradePlanData(list);
        }
        return set;
    }
    
    /**
     * 查询当月签约计划同步表
     */
	@Override
	public List<MonthTradePlanDto> getMonthTradePlanList() {
		return monthTradePlanMapper.selectMonthTradePlanList();
	}

    public static void main(String[] args) {
        log.info(LocalDateTime.now()+"");
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String createDate1 = "2019-12-01"+" 00:00:00";
        LocalDateTime signDate = LocalDateTime.parse(createDate1, formatter);
        log.info(signDate+"");

    }
}
